home *** CD-ROM | disk | FTP | other *** search
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Newsgroups: comp.lang.c
- Subject: Re: Command line arg and
- Date: 5 Feb 1996 18:38:50 GMT
- Organization: Ripco Communications, Inc.
- Message-ID: <4f5irq$44v@gail.ripco.com>
- NNTP-Posting-Host: golden.ripco.com
-
- Chris Frenck <cfrenck@soleil.acomp.usf.edu>
- in <311273B8.25BF@soleil.acomp.usf.edu> asks:
-
- >I am a beginning C student and have run into a bit of a problem. When compiling
- >on GCC
- >(required) I get a segmentation fault. If I compile with MSVC++ at home it runs
- >fine.
- >Could someone please tell me what the error is?
-
-
- First, here's something else to fix in check():
-
- #include <stdlib.h> /* mha - added */
- /*....*/
- if (argc > 1) {
- for (count = 1; count < argc; count++); /* counts arguments */
- } else { /* mha - added */
- printf("No command line arguments to process.");
- exit(EXIT_FAILURE); /* mha - added */
- } /* mha - added */
-
- The segfault you saw does not actually occur in palin(), but in
- reverse(). Check the following and see if it helps you. It will make
- the segfault go away, but both check() and reverse() (and palin() and
- forward()) could stand more work. One reason is that `count' is
- actually useless. All the information `count' contains is also contained
- in `argc'.
-
- void reverse(int count, char *argv[])
- {
- /* Variable Declarations */
- /* mha - removed "char *argument;" */
- /* mha - removed "int tempctr;" (see "for" comment) */
- char *revphrase;
-
- /* Function Prototype */
- char palin(char[], char[]);
-
- /* mha - note that tempctr has no function at all in
- * "for (tempctr = count; count > 1; count--) {", etc.
- * replaced for pedagogical reasons with the following. Note that
- * without the initial count--, the first argv[count] is a null
- * pointer (in "prog backward foobar", count = 3 and "foobar" is
- * actually argv[2]) */
-
- for (count--; count > 1; count--) {
- /* mha - removed "argument = argv[count];"); */
-
- revphrase = malloc(strlen(argv[count]) + 1);
- /* mha - the above added; without this no space has been
- * allocated for revphrase */
-
- palin(argv[count], revphrase);
- /* mha - the above was "palin(argument, revphrase);" */
-
- printf("%s ", revphrase);
-
- /* mha - removed "tempctr++;" since not relevant (see "for"
- * comment) */
-
- free(revphrase); /* mha - added */
- }
- }
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-